﻿using System;
using System.Collections.Generic;
using System.Linq;
using NFine.Code;
using NFine.Data;
using NFine.Domain.Entity.SystemManage;
using NFine.Domain.IRepository.SystemManage;
using NFine.Domain._03_Entity.Biz;
using NFine.Domain._04_IRepository.Biz;
using NFine.Repository.Biz;
using NFine.Repository.SystemManage;

namespace NFine.Application.Biz
{
    public class UserWithdrawalsApp
    {
        private IUserWithdrawalsRepository userWithdrawalsRepository = new UserWithdrawalsRepository();
        private IUserRepository userRepository = new UserRepository();
        private IUserPropertyRepository userPropertyRepository = new UserPropertyRepository();
        private IUserBankRepository userBankRepository = new UserBankRepository();
        private IUserDetailRepository userDetailRepository = new UserDetailRepository();
        private IMemberBonusRepository memberBonusRepository = new MemberBonusRepository();


        public List<UserWithdrawalsEntity> GetListForWithdrawals(Pagination pagination, int type,  string start, string end, string userId = "")
        {
            var expression = ExtLinq.True<UserWithdrawalsEntity>();

            if (type != -2)
            {
                expression = expression.And(t => t.F_WithdrawalsStatus == type);
            }

            

            if (!string.IsNullOrEmpty(userId))
            {
                expression = expression.And(t => t.F_UserId == userId);
            }

            if (!string.IsNullOrEmpty(start))
            {
                DateTime date = Convert.ToDateTime(start);
                expression = expression.And(a => a.F_WithdrawalsTime >= date);
            }
            if (!string.IsNullOrEmpty(end))
            {
                DateTime date = Convert.ToDateTime(end).AddDays(1).AddSeconds(-1);
                expression = expression.And(a => a.F_WithdrawalsTime <= date);
            }
            List<UserWithdrawalsEntity> list = userWithdrawalsRepository.FindList(expression, pagination).OrderByDescending(a => a.F_WithdrawalsTime).ToList();
            foreach (var temp in list)
            {
                UserEntity userEntity = userRepository.FindEntity(temp.F_UserId);
                if (userEntity != null)
                {
                    temp.UserName = userEntity.F_Account;
                    temp.RealName = userEntity.F_RealName;
                    temp.CellPhone = userEntity.F_MobilePhone;
                }
                else
                {
                    temp.UserName = "";
                    temp.RealName = "";
                    temp.CellPhone = "";
                }

                if (!string.IsNullOrEmpty(temp.F_BankId))
                {
                    UserBankEntity userBankEntity = userBankRepository.FindEntity(temp.F_BankId);
                    if (userBankEntity != null)
                    {
                        temp.OpeningBank = userBankEntity.F_OpeningBank;
                        temp.AccountName = userBankEntity.F_AccountName;
                        temp.Account = userBankEntity.F_Account;
                    }
                }
                
            }

            return list;
        }

        public decimal GetAllTixianMoney()
        {
            decimal? result = userWithdrawalsRepository.GetAllTixianMoney();
            return result == null ? 0.00M : result.Value;
        }

        public int DoWithdrawals(string Id)
        {
            UserWithdrawalsEntity userWithdrawalsEntity = userWithdrawalsRepository.FindEntity(Id);
            decimal money = userWithdrawalsEntity.F_WithdrawalsMoney.Value;
            //TODO  微信转账
            
            //TODO
            
            //更新打款状态
            userWithdrawalsEntity.F_WithdrawalsStatus = 1;
            userWithdrawalsEntity.F_DoTime = DateTime.Now;
            //int result = userWithdrawalsRepository.Update(userWithdrawalsEntity);
            var curmoney=memberBonusRepository.GetMemberCurAmount(userWithdrawalsEntity.F_UserId);

            var user = userRepository.FindEntity(userWithdrawalsEntity.F_UserId);

            MemberBonusEntity momberBonuserP = new MemberBonusEntity();
            momberBonuserP.F_Id = Common.GuId();
            momberBonuserP.F_MemberId = userWithdrawalsEntity.F_UserId;
            momberBonuserP.F_OrderId = userWithdrawalsEntity.F_Id;
            momberBonuserP.F_BonusAmount = money*-1;
            momberBonuserP.F_BonusLevel = 3;//提现
            momberBonuserP.F_BonusTime = DateTime.Now;
            momberBonuserP.F_BonusType = 1;//提现
            momberBonuserP.F_CurAmount = curmoney- money;
            momberBonuserP.F_bz = string.Format("会员[{0}({1})]的提现", user.F_RealName, user.F_Account);

            MemberBonusEntity momberBonuserSXF = new MemberBonusEntity();
            momberBonuserSXF.F_Id = Common.GuId();
            momberBonuserSXF.F_MemberId = userWithdrawalsEntity.F_UserId;
            momberBonuserSXF.F_OrderId = userWithdrawalsEntity.F_Id;
            //提现手续费调整为1.5%  也就是千分之15
            momberBonuserSXF.F_BonusAmount = Math.Round(money * 15 / 1000,2)*-1;
            momberBonuserSXF.F_BonusLevel = 4;//提现费
            momberBonuserSXF.F_BonusTime = momberBonuserP.F_BonusTime.AddSeconds(1);
            momberBonuserSXF.F_BonusType = 2;//提现手续费
            momberBonuserSXF.F_CurAmount = curmoney - money + momberBonuserSXF.F_BonusAmount;
            momberBonuserSXF.F_bz = string.Format("会员[{0}({1})]的提现手续费", user.F_RealName, user.F_Account);

            using (var db = new RepositoryBase().BeginTrans())
            {
                db.Update(userWithdrawalsEntity);
                db.Insert(momberBonuserP);
                db.Insert(momberBonuserSXF);
                return db.Commit();
            }
        }

        public string getOpenIdById(string id)
        {
            UserWithdrawalsEntity userWithdrawalsEntity = userWithdrawalsRepository.FindEntity(id);
            return userWithdrawalsEntity.F_OpenId;
        }

        public int Withdrawals(decimal money, string userId, string bankId)
        {
            DateTime curTime = System.DateTime.Now;
            using (var db = new RepositoryBase().BeginTrans())
            {
                // 插入提现记录
                UserWithdrawalsEntity userWithdrawalsEntity = new UserWithdrawalsEntity();
                userWithdrawalsEntity.F_Id = Common.GuId();
                userWithdrawalsEntity.F_UserId = userId;
                userWithdrawalsEntity.F_OpenId = "";
                userWithdrawalsEntity.F_WithdrawalsTime = curTime;
                userWithdrawalsEntity.F_WithdrawalsMoney = money;
                userWithdrawalsEntity.F_WithdrawalsStatus = 0;
                userWithdrawalsEntity.F_BankId = bankId;
                userWithdrawalsEntity.F_SerialNumber = Common.CreateNo();
                db.Insert(userWithdrawalsEntity);

                //更新用户资产表
                var expression = ExtLinq.True<UserPropertyEntity>();
                List<UserPropertyEntity> list = new List<UserPropertyEntity>();

                expression = expression.And(t => t.F_UserId == userId);
                list = userPropertyRepository.FindList(expression).ToList();

                if (list != null && list.Count > 0)
                {
                    list[0].F_Property -= money;
                    list[0].F_LastModifyTime = DateTime.Now;
                    db.Update(list[0]);
                }

                int re = db.Commit();

                if (re > 0)
                {
                    return 1;
                }
                return -1;
            }
            return 0;
        }

    }
}